package cn.wolfcode.car.controller;

import cn.wolfcode.car.domain.Permission;
import cn.wolfcode.car.qo.PermissionQueryObject;
import cn.wolfcode.car.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import result.JsonResult;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.*;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private ApplicationContext ctx;

    @RequestMapping("/list")
    public String list(Model model, PermissionQueryObject qo) {
        PageInfo<Permission> list = permissionService.list(qo);
        System.out.println(list);
        model.addAttribute("result", list);
        return "permission/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Permission permission, HttpServletRequest request) {

        Map map = request.getParameterMap();
        Set keSet = map.entrySet();
        for (Iterator itr = keSet.iterator(); itr.hasNext(); ) {
            Map.Entry me = (Map.Entry) itr.next();
            Object ok = me.getKey();
            Object ov = me.getValue();
            String[] value = new String[1];
            if (ov instanceof String[]) {
                value = (String[]) ov;
            } else {
                value[0] = ov.toString();
            }

            for (int k = 0; k < value.length; k++) {
                System.out.println(ok + "=" + value[k]);
            }
        }

        if (permission.getId() == null) {
            permissionService.save(permission);
        } else {
            permissionService.update(permission);
        }
        return "redirect:/permission/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        permissionService.delete(id);
        System.out.println(JsonResult.success(null));
        return JsonResult.success(null);
    }

    @RequestMapping("/reload")
    @ResponseBody
    public Object reload() {
        List<String> permissions = permissionService.selectExpression();

        //1.获取所有controller
        Map<String, Object> beans = ctx.getBeansWithAnnotation(Controller.class);
        System.out.println("beans=" + beans.toString());
        Collection<Object> values = beans.values();
        System.out.println("values=" + values.toString());
        for (Object value : values) {
            System.out.println("value=" + value.toString());
            //如果当前对象不是代理类对象,说明是一个普通的controller,该controller中一定没有RequiredPermissions注解
            if (!AopUtils.isCglibProxy(value)) {
                continue;
            }
            /**'
             * value.getClass() 代理子类的字节码
             * value.getClass().getSuperclass() 真正的controller字节码
             */
            Class<?> controller = value.getClass().getSuperclass();
            Method[] methods = controller.getDeclaredMethods();
            for (Method method : methods) {
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                if (annotation == null) {
                    continue;
                }
                String[] v = annotation.value();
                String expression = v[0];
                String name = v[1];

                if (!permissions.contains(expression)) {
                    Permission permission = new Permission(null, name, expression);
                    permissionService.save(permission);
                }

            }
        }
        return JsonResult.success(null);
    }
}
